#include <asm/segment.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>

char psinfo_buffer[1024];

int sprintf(char *buf, const char *fmt, ...)
{
    va_list args;
    int i;

    va_start(args, fmt);

    i=vsprintf(buf, fmt, args);

    va_end(args);

    return i;
}
int idonothing(char * buf, int Count,off_t * f_pos)
{
    int k;
    for(k=0; k<Count; k++)
    {
        if(psinfo_buffer[k+(*f_pos)]=='\0')
            break;
        put_fs_byte(psinfo_buffer[k+(*f_pos)],buf+k+(*f_pos));
    }
    (*f_pos)=(*f_pos)+k;
    return k;
}

int psinfo_read(int dev,char * buf,int Count,off_t * f_pos)
{
    struct task_struct *p;
    int i;
    int n=0;

    memset(psinfo_buffer,'\0',sizeof(psinfo_buffer));

    if(dev==0)
    {
        n+=sprintf(&psinfo_buffer[n],"pid\tstate\tfather\tcounter\tstart_time\n");
        for(i=0; i<64; i++)
        {
            p=task[i];
            if(p!=NULL)
            {
                n+=sprintf(&psinfo_buffer[n],"%d\t%d\t%d\t%d\t%d\n",p->pid,p->state,p->father,p->counter,p->start_time);
            }
        }
    }

    return idonothing(buf,Count,f_pos);
}

int hdinfo_read(int dev,char * buf,int Count,off_t * f_pos)
{
    struct super_block * sb;
    int n=0;

    sb=get_super(dev);

    memset(psinfo_buffer,'\0',sizeof(psinfo_buffer));

    n+=sprintf(&psinfo_buffer[n],"total_blocks:%d;\n",sb->s_nzones);
    n+=sprintf(&psinfo_buffer[n],"free_blocks :%d;\n",sb->s_nzones-sb->s_zmap_blocks);
    n+=sprintf(&psinfo_buffer[n],"used_blocks :%d;\n",sb->s_zmap_blocks);
    n+=sprintf(&psinfo_buffer[n],"total_inodes:%d;\n",sb->s_ninodes);

    return idonothing(buf,Count,f_pos);
}

int inodeinfo_read(int dev,char * buf,int Count,off_t * f_pos)
{
    struct m_inode * inode;
    int n=0;
    int j=0;

    memset(psinfo_buffer,'\0',sizeof(psinfo_buffer));

    for(j=1; j<7; j++)
    {
        inode = iget(dev,j);
        n+=sprintf(&psinfo_buffer[n],"inr:%d;zone[0]:%d\n",j,inode->i_zone[0]);
    }

    return idonothing(buf,Count,f_pos);
}

